﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysGroup
    {
        public int Add(Chain.Model.SysGroup model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysGroup(");
            builder.Append("GroupName,GroupRemark,ParentGroupID,ParentIDStr,IsPublic,CreateUserID)");
            builder.Append(" values (");
            builder.Append("@GroupName,@GroupRemark,@ParentGroupID,@ParentIDStr,@IsPublic,@CreateUserID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupName", SqlDbType.NVarChar, 50), new SqlParameter("@GroupRemark", SqlDbType.NVarChar, 200), new SqlParameter("@ParentGroupID", SqlDbType.Int, 4), new SqlParameter("@ParentIDStr", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@IsPublic", SqlDbType.Bit, 1), new SqlParameter("@CreateUserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GroupName;
            cmdParms[1].Value = model.GroupRemark;
            cmdParms[2].Value = model.ParentGroupID;
            cmdParms[3].Value = model.ParentIDStr;
            cmdParms[4].Value = model.IsPublic;
            cmdParms[5].Value = model.CreateUserID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.SysGroup DataRowToModel(DataRow row)
        {
            Chain.Model.SysGroup group = new Chain.Model.SysGroup();
            if (row != null)
            {
                if ((row["GroupID"] != null) && (row["GroupID"].ToString() != ""))
                {
                    group.GroupID = int.Parse(row["GroupID"].ToString());
                }
                if (row["GroupName"] != null)
                {
                    group.GroupName = row["GroupName"].ToString();
                }
                if (row["GroupRemark"] != null)
                {
                    group.GroupRemark = row["GroupRemark"].ToString();
                }
                if (row["ParentGroupID"] != null)
                {
                    group.ParentGroupID = int.Parse(row["ParentGroupID"].ToString());
                }
                if (row["ParentIDStr"] != null)
                {
                    group.ParentIDStr = row["ParentIDStr"].ToString();
                }
                if (row["IsPublic"] != null)
                {
                    group.IsPublic = bool.Parse(row["IsPublic"].ToString());
                }
                if (row["CreateUserID"] != null)
                {
                    group.CreateUserID = int.Parse(row["CreateUserID"].ToString());
                }
            }
            return group;
        }

        public bool Delete(int GroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysGroup ");
            builder.Append(" where GroupID=@GroupID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GroupID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string GroupIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysGroup ");
            builder.Append(" where GroupID in (" + GroupIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int GroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysGroup");
            builder.Append(" where GroupID=@GroupID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GroupID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetGroupAuthority(int GroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM V_GroupAuthority ");
            builder.Append(" where GroupID=" + GroupID);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GroupID,GroupName,GroupRemark,ParentGroupID,ParentIDStr,IsPublic,CreateUserID,SysUser.UserName ");
            builder.Append(" FROM SysGroup left join SysUser on SysGroup.CreateUserID = SysUser.UserID ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" GroupID,GroupName,GroupRemark,ParentGroupID,ParentIDStr,IsPublic,CreateUserID ");
            builder.Append(" FROM SysGroup ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.GroupID desc");
            }
            builder.Append(")AS Row, T.*  from SysGroup T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListS(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = "SysGroup";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GroupID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("GroupID", "SysGroup");
        }

        public Chain.Model.SysGroup GetModel(int GroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 GroupID,GroupName,GroupRemark,ParentGroupID,ParentIDStr,IsPublic,CreateUserID from SysGroup ");
            builder.Append(" where GroupID=@GroupID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GroupID;
            new Chain.Model.SysGroup();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysGroup ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysGroup model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysGroup set ");
            builder.Append("GroupName=@GroupName,");
            builder.Append("GroupRemark=@GroupRemark,");
            builder.Append("ParentGroupID=@ParentGroupID,");
            builder.Append("ParentIDStr=@ParentIDStr,");
            builder.Append("IsPublic=@IsPublic,");
            builder.Append("CreateUserID=@CreateUserID");
            builder.Append(" where GroupID=@GroupID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupName", SqlDbType.NVarChar, 50), new SqlParameter("@GroupRemark", SqlDbType.NVarChar, 200), new SqlParameter("@ParentGroupID", SqlDbType.Int, 4), new SqlParameter("@ParentIDStr", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@IsPublic", SqlDbType.Bit, 1), new SqlParameter("@CreateUserID", SqlDbType.Int, 4), new SqlParameter("@GroupID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GroupName;
            cmdParms[1].Value = model.GroupRemark;
            cmdParms[2].Value = model.ParentGroupID;
            cmdParms[3].Value = model.ParentIDStr;
            cmdParms[4].Value = model.IsPublic;
            cmdParms[5].Value = model.CreateUserID;
            cmdParms[6].Value = model.GroupID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool UpdateParentId(int OldParentGroupID, int NewParentGroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysGroup set ");
            builder.Append("ParentGroupID=@NewParentGroupID");
            builder.Append(" where ParentGroupID=@OldParentGroupID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewParentGroupID", SqlDbType.Int, 4), new SqlParameter("@OldParentGroupID", SqlDbType.Int, 4) };
            cmdParms[0].Value = NewParentGroupID;
            cmdParms[1].Value = OldParentGroupID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

